import { Component, OnInit } from '@angular/core';
import {WasherService} from '../shared/washer.service';
import {ActivatedRoute, Router} from '@angular/router';
import {Washer, WasherToAdd, WasherToUpdate} from '../shared/washer.model';
import {MatSnackBar} from '@angular/material';

@Component({
  selector: 'app-washer-detail',
  templateUrl: './washer-detail.component.html',
  styleUrls: ['./washer-detail.component.css']
})
export class WasherDetailComponent implements OnInit {

  public id: number;
  public firstName: string;
  public lastName: string;
  public phoneNumber: string;
  public openId: string;
  public identityId: string;
  public gender: string;
  public level: string;
  public levelId: string;
  public isBusy: boolean;

  public isEditingMode = false;
  public isAddingMode = false;

  constructor(
    private washerService: WasherService,
    private snackBar: MatSnackBar,
    private router: Router,
    private activatedRoute: ActivatedRoute) { }

  ngOnInit() {
    const washerId = this.activatedRoute.snapshot.paramMap.get('id');
    if (Number(washerId) > 0) { // view mode
      this.id = this.washerService.currentWasher.id;
      this.firstName = this.washerService.currentWasher.firstName;
      this.lastName = this.washerService.currentWasher.lastName;
      this.phoneNumber = this.washerService.currentWasher.phoneNumber;
      this.openId = this.washerService.currentWasher.openId;
      this.identityId = this.washerService.currentWasher.identityId;
      this.gender = this.washerService.currentWasher.gender;
      this.isBusy = this.washerService.currentWasher.busy;
      this.levelId = this.washerService.currentWasher.levelId;
      this.level = this.washerService.currentWasher.level;
      // this.isEditingMode = true;
    } else if (Number(washerId) === -1) { // add mode
      this.isAddingMode = true;
    }
  }
  onEditClick(target) {
    this.isEditingMode = true; // edit mode
  }
  onSaveClick(target) {
    if (this.isAddingMode) {
      const washer = new WasherToAdd(this.openId, this.lastName + this.firstName, this.lastName,
        this.firstName, this.phoneNumber, 0, this.identityId);
      this.washerService.addWasher(washer).subscribe(result => {
        if (result) {
          this.openSnackBar('添加成功！', '');
          this.router.navigate(['washers']);
        } else {
          this.openSnackBar('添加错误！', '');
        }
      }, result => {
        this.openSnackBar('添加失败！', '');
      }, () => {
        this.isEditingMode = false;
      });
    } else if (this.isEditingMode) {
      const washer = new WasherToUpdate(this.id, this.lastName + this.firstName, this.firstName, this.lastName,
        this.phoneNumber, this.identityId, this.gender, this.levelId, this.isBusy);
      this.washerService.updateWasher(washer).subscribe(result => {
        if (result) {
          this.openSnackBar('更新成功！', '');
        } else {
          this.openSnackBar('更新错误！', '');
        }
      }, result => {
        this.openSnackBar('更新失败！', '');
        },
        () => {
        this.isAddingMode = false;
        });
    } else {
      this.openSnackBar('请先保存！', '');
    }
  }
  getOpenIdClicked(target) {
    this.washerService.getOpenId(this.phoneNumber).subscribe(result => {
      this.openId = result;
      this.openSnackBar('获取OpenId成功！', '');
    }, result => {
      this.openSnackBar('获取OpenId失败！', '');
    });
  }
  openSnackBar(message: string, action: string) {
    this.snackBar.open(message, action, {
      duration: 2000
    });
  }
}
